
;; for melpa
(require 'package)

;; mirrors
;; ;; emacs-china
(setq package-archives '(("gnu"   . "http://elpa.emacs-china.org/gnu/")
			             ("Org"   . "http://elpa.emacs-china.org/org/")
			             ("melpa" . "http://elpa.emacs-china.org/melpa/")))

;; (setq package-archives '(("gnu" . "http://1.15.88.122/gnu/")
;;                          ("melpa" . "http://1.15.88.122/melpa/")))

;; ;; Tencent
;; (setq package-archives '(("gnu"   . "http://mirrors.cloud.tencent.com/elpa/gnu/")
;; 			 ("Org"   . "http://mirrors.cloud.tencent.com/elpa/org/")
;; 			 ("melpa" . "http://mirrors.cloud.tencent.com/elpa/melpa/")))

;; ;; TUNA
;; (setq package-archives
;;       '(("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
;; 	    ("org-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
;; 	    ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))

;; ;; USTC
;; (setq package-archives '(("gnu" . "https://mirrors.ustc.edu.cn/elpa/gnu/")
;;                          ("melpa" . "https://mirrors.ustc.edu.cn/elpa/melpa/")
;;                          ("melpa-stable" . "https://mirrors.ustc.edu.cn/elpa/melpa-stable/")
;;                          ("org" . "https://mirrors.ustc.edu.cn/elpa/org/")))

;; (add-to-list 'load-path "~/.emacs.d/site-lisp/")

;; list the wanted packages
(setq
 p1uxtar-package-list
 '(ace-pinyin ace-window adoc-mode aggressive-indent all-the-icons
              all-the-icons-ivy-rich amx auctex bing-dict c-eldoc
              centaur-tabs cdlatex cfrs circadian cnfonts company
              company-box company-dict company-prescient
              company-posframe company-tabnine counsel
              counsel-projectile csv-mode dashboard diff-hl diminish
              diredfl doom-themes eglot electric-operator eldoc-box
              el2org ess ess-r-insert-obj ess-smart-equals
              ess-view-data fcitx find-file-in-project flycheck
              flycheck-posframe format-all fuz gitconfig-mode
              gitignore-mode highlight-indentation highlight-numbers
              ht htmlize hydra ialign ivy ivy-fuz ivy-posframe
              ivy-prescient ivy-rich ivy-yasnippet jsonrpc
              kaolin-themes linum-relative lua-mode magic-latex-buffer
              magit markup-faces meow move-dup multiple-cursors
              multi-term mwim opencc ox-gfm pfuture prescient prettier
              projectile polymode poly-markdown poly-R posframe pyim
              pyvenv rainbow-delimiters rainbow-mode rotate selected
              separedit shfmt smart-mode-line swiper symbol-overlay
              treemacs treemacs-magit treemacs-icons-dired
              treemacs-projectile undo-tree use-package which-key
              winum xr yaml-mode yasnippet-snippets youdao-dictionary))

;; activate all the packages
(package-initialize)

;; fetch the list of packages available
(unless package-archive-contents
  (package-refresh-contents))

;; install the missig packages
(dolist (package p1uxtar-package-list)
  (unless (package-installed-p package)
    (package-install package)))


;; ;; -------- auto-package-update --------
;; (use-package auto-package-update
;;   :if window-system
;;   :config
;;   (setq auto-package-update-interval 14
;; 	auto-package-update-delete-old-versions t
;; 	auto-package-update-hide-results t)
;;   (auto-package-update-maybe))


;; ;; emacs.stackexchange.com/questions/18253/how-does-package-autoremove-decide-which-packages-to-remove
;; (defun package-autoremove ()
;;   "Remove packages that are no more needed.
;;         Packages that are no more needed by other packages in
;;         `package-selected-packages' and their dependencies
;;         will be deleted."
;;   (interactive)
;;   ;; If `package-selected-packages' is nil, it would make no sense to
;;   ;; try to populate it here, because then `package-autoremove' will
;;   ;; do absolutely nothing.
;;   (when (or package-selected-packages
;;             (yes-or-no-p
;; 	     (format-message
;; 	      "`package-selected-packages' is empty! Really remove ALL packages? ")))
;;     (let ((removable (package--removable-packages)))
;;       (if removable
;; 	  (when (y-or-n-p
;; 		 (format "%s packages will be deleted:\n%s, proceed? "
;; 			 (length removable)
;; 			 (mapconcat #'symbol-name removable ", ")))
;;             (mapc (lambda (p)
;;                     (package-delete (cadr (assq p package-alist)) t))
;; 		  removable))
;;         (message "Nothing to autoremove")))))

;; (defun package--removable-packages ()
;;   "Return a list of names of packages no longer needed.
;; These are packages which are neither contained in
;; `package-selected-packages' nor a dependency of one that is."
;;   (let ((needed (cl-loop for p in package-selected-packages
;;                          if (assq p package-alist)
;;                          ;; `p' and its dependencies are needed.
;;                          append (cons p (package--get-deps p)))))
;;     (cl-loop for p in (mapcar #'car package-alist)
;;              unless (memq p needed)
;;              collect p)))
